#include "Cube.h"
#include "MyVertex.h"

namespace CAD_CAM
	{
	Cube::Cube(void): Primitive(8, 24)
	{
		build();
	}

	Cube::Cube(float size): Primitive(size, 8, 24)
	{
		build();
	}

	Cube::Cube(QVector3D position, float size): Primitive(position, size, 8, 24)
	{
		build();
	}

	Cube::Cube(QVector3D position): Primitive(position, 8, 24)
	{
		build();
	}

	Cube::~Cube(void)
	{
	}

	void Cube::build()
	{
		// front wall
		verticesArray[0] = MyVertex(-0.5f, -0.5f, 0.5f, color);
		verticesArray[1] = MyVertex(0.5f, -0.5f, 0.5f, color);
		verticesArray[2] = MyVertex(0.5f, 0.5f, 0.5f, color);
		verticesArray[3] = MyVertex(-0.5f, 0.5f, 0.5f, color);
	
		// back wall
		verticesArray[4] = MyVertex(-0.5f, -0.5f, -0.5f, color);
		verticesArray[5] = MyVertex(0.5f, -0.5f, -0.5f, color);
		verticesArray[6] = MyVertex(0.5f, 0.5f, -0.5f, color);
		verticesArray[7] = MyVertex(-0.5f, 0.5f, -0.5f, color);

		// 3 linie wychodzace z przedniego, lewego dolnego wierzcholka
		indicesArray.append(0);
		indicesArray.append(1);
		indicesArray.append(0);
		indicesArray.append(3);
		indicesArray.append(0);
		indicesArray.append(4);
		// 3 linie wychodzace z przedniego, prawego gornego wierzcholka
		indicesArray.append(2);
		indicesArray.append(1);
		indicesArray.append(2);
		indicesArray.append(3);
		indicesArray.append(2);
		indicesArray.append(6);
		// 3 linie wychodzace z tylnego, lewego gornego wierzcholka
		indicesArray.append(7);
		indicesArray.append(3);
		indicesArray.append(7);
		indicesArray.append(4);
		indicesArray.append(7);
		indicesArray.append(6);
		// 3 linie wychodzace z tylnego, prawego dolnego wierzcholka
		indicesArray.append(5);
		indicesArray.append(1);
		indicesArray.append(5);
		indicesArray.append(4);
		indicesArray.append(5);
		indicesArray.append(6);
	}

	void Cube::DrawShader(QGLShaderProgram& shaderProgram, QMatrix4x4& viewMatrix, QMatrix4x4& projectionMatrix)
	{
		if(isInited == false)
			initBuffers();
		mVertexVBO.bind();
		mIndexVBO.bind();
	
		QMatrix4x4 modelMatrix;
		modelMatrix.translate(translation);
		//mModelMatrix *= rotationMatrix;
		modelMatrix.scale(sizeScale);

		shaderProgram.bind();

		int inPositionLocation = shaderProgram.attributeLocation("inPosition");
		int inColorLocation = shaderProgram.attributeLocation("inColor");

		int inModelMatrixLocation = shaderProgram.uniformLocation("inModelMatrix");
		int inViewMatrixLocation = shaderProgram.uniformLocation("inViewMatrix");
		int inProjectionMatrixLocation = shaderProgram.uniformLocation("inProjectionMatrix");

		shaderProgram.enableAttributeArray(inPositionLocation);
		shaderProgram.enableAttributeArray(inColorLocation);

		shaderProgram.setAttributeBuffer(inPositionLocation, GL_FLOAT, 0, 3, sizeof(MyVertex));
		shaderProgram.setAttributeBuffer(inColorLocation, GL_FLOAT, 12, 3, sizeof(MyVertex));
		shaderProgram.setUniformValue(inModelMatrixLocation, modelMatrix);
		shaderProgram.setUniformValue(inViewMatrixLocation, viewMatrix);
		shaderProgram.setUniformValue(inProjectionMatrixLocation, projectionMatrix);

		glDrawElements(GL_LINES, indicesArray.count(), GL_UNSIGNED_INT, 0);

		shaderProgram.disableAttributeArray(inPositionLocation);
		shaderProgram.disableAttributeArray(inColorLocation);
	
		shaderProgram.release();
	
		mVertexVBO.release();
		mIndexVBO.release();
	}
}